package com.eco.fanliapp.job.unBindingOrder;

import com.eco.fanliapp.common.OrderTaskUtils;
import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.IUnBindingOrderInfoHandleService;
import com.eco.fanliapp.utils.DateCalculateUtils;
import com.eco.fanliapp.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 拉取2个月内积分订单
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class UnBindingOrderTask implements Job, Serializable {

    private static final Logger log = LoggerFactory.getLogger(UnBindingOrderTask.class);
    private String timeFlagIntegralOrder ="";

    @Autowired
    private IUnBindingOrderInfoHandleService iUnBindingOrderInfoHandleService;

    @Autowired
    private OrderTaskUtils orderTaskUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    public UnBindingOrderTask(){

    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //取得job详情
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap =  jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }
        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long beginTime = System.currentTimeMillis();
        log.info("***************************************开始执行定时任务-拉取 mingxi订单 定时任务-UnBindingOrderTask：executeCount="+executeCount+"--beginTime="+beginTime);

        //获取淘宝联盟账号-排除已过期的  61024084fd3efa8c1ec943cdd6f1a1c3fcadeff1ce0eaed757011687
        List<TaobaoScheme> taobaoSchemes = orderTaskUtils.getUsedTaobaoSchemeList();
        //开始时间、结束时间
        String startTime = OrderTaskUtils.getSystemTime();
        String endTime = OrderTaskUtils.getNowTime();
        iUnBindingOrderInfoHandleService.handlePayOrder(startTime, endTime, taobaoSchemes, 1);
        iUnBindingOrderInfoHandleService.handleCompleteOrder(startTime, endTime, taobaoSchemes, 1);
        iUnBindingOrderInfoHandleService.handleFailOrder(startTime, endTime, taobaoSchemes, 1);
        iUnBindingOrderInfoHandleService.handleSettleOrder(startTime, endTime, taobaoSchemes, 1);

        //切分时间
//        List<String> list = handleTime(timeFlagIntegralOrder);
//        String startTime = null;
//        String endTime = null;
//        if(CollectionUtils.isNotEmpty(list)){
//            for(String time:list){
//                if (startTime == null) {
//                    startTime = time;
//                    continue;
//                }
//                endTime = time;
//                Long dealEndTime = DateUtils.datestr2LongByFormt(endTime, null);
//                if (dealEndTime >= (System.currentTimeMillis() - 1000L * 60 * 20)) {
//                    continue;
//                }
//                log.info("开始拉取1月mingxi订单-开始时间{}、结束时间{}", startTime, endTime);
////                处理订单
//                iUnBindingOrderInfoHandleService.handlePayOrder(startTime, endTime, taobaoSchemes, 1);
//                iUnBindingOrderInfoHandleService.handleCompleteOrder(startTime, endTime, taobaoSchemes, 1);
//                iUnBindingOrderInfoHandleService.handleFailOrder(startTime, endTime, taobaoSchemes, 1);
//                iUnBindingOrderInfoHandleService.handleSettleOrder(startTime, endTime, taobaoSchemes, 1);
//                log.info("结束拉取 mingxi订单 -开始时间{}、结束时间{}", startTime, endTime);
//                startTime = time;
//            }
//        }
        log.info("***************************************结束执行定时任务-拉取 mingxi订单 定时任务-UnBindingOrderTask：executeCount=" + executeCount + "--endTime=" + System.currentTimeMillis() + "--耗时=" + (System.currentTimeMillis() - beginTime));

    }

    /**
     * 处理时间
     * @param timeFlag
     * @return
     */
    public static List<String> handleTime(String timeFlag){
        //当前日期参照点
        Date currentTime = new Date();
        String currentTimeStr = DateUtils.date2Str(currentTime, null);
        if (timeFlag == null || "".equals(timeFlag)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentTime);
//            calendar.add(Calendar.MINUTE, -(24 * 60 * 90 + 20));
            calendar.add(Calendar.MINUTE, -(60 + 20));
//            calendar.add(Calendar.MINUTE, -(60));
            timeFlag = DateUtils.date2Str(calendar.getTime(), null);
            log.info("拉取明细订单定时任务-开始1天时间参照点{}", timeFlag);
        }
        //切割时间段(交易类型 M/D/H/N -->每月/每天/每小时/每分钟,多久分成一份，开始时间，结束时间)
        List<String> list = DateCalculateUtils.cutDate("N",1200, timeFlag, currentTimeStr);
        return list;
    }

    public static void main(String[] args) throws ParseException {
//        String timeFlag = "";
//        List<String> strings = handleTime(timeFlag);
//        for(String s : strings){
//            System.out.println(s);
//        }
    }


}
